/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tolls;

import avenue.MyWindowEvent;
import avenue.Session;
import avenue.messageBox;
import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.DBManager;
import model.ProgramRate;
import model.Vehicle;
import program.EditProgramForm;

/**
 *
 * @author Aggelos
 */
public class CollectTollForm extends javax.swing.JFrame {
private EntityManager em;
    /**
     * Creates new form CollectTollForm
     */
    public CollectTollForm() {
        
        em = DBManager.em;
        if ( !(em.getTransaction().isActive()) ){
            em.getTransaction().begin();
        }
        
        initComponents();
          getContentPane().setBackground(Color.white);
        //Στοιχιση στο κέντρο 24/2/2013
        setLocationRelativeTo(null);
              jLabel3.setVisible(false);
              jLabel4.setVisible(false);
              jLabel5.setVisible(false);
              jmoney.setVisible(false);
              jButton2.setVisible(false);
              jButton1.setVisible(false);
              jButton4.setVisible(false);
              jTextField1.setText(Session.loginUser.getStationId().getDescription());
   
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        query1 = java.beans.Beans.isDesignTime() ? null : em.createQuery("select v from Vehicle v");
        list1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query1.getResultList());
        collectionquery = java.beans.Beans.isDesignTime() ? null : em.createQuery("select co from Collection co");
        colllist = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query1.getResultList());
        arKikloforias = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jbarcode = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jmoney = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/flag_green.png"))); // NOI18N
        jButton1.setText("Διέλευση");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jbarcode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbarcodeActionPerformed(evt);
            }
        });

        jLabel2.setText("Αρ. Κάρτας: ");

        jLabel1.setText("Αρ. Κυκλοφορίας: ");

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/coins.png"))); // NOI18N
        jButton2.setText("Φορτιση Κάρτας");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setText("Υπόλοιπο Κάρτας: ");

        jmoney.setEnabled(false);

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/imagesCAL4A9SX.jpg"))); // NOI18N

        jLabel9.setFont(new java.awt.Font("Snap ITC", 1, 24)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(0, 3, 130));
        jLabel9.setText("TelePass 1.0");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel7.setText("Διέλευση");

        jLabel4.setForeground(new java.awt.Color(255, 51, 51));
        jLabel4.setText("jLabel4");

        jLabel5.setText("jLabel4");

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        jButton4.setText("Ακυρο");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cog.png"))); // NOI18N
        jButton3.setText("Ελεγχος");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTextField1.setText("jTextField1");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel6.setText("Σταθμός: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 570, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(366, 366, 366)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1)
                        .addGap(4, 4, 4)
                        .addComponent(arKikloforias, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(162, 162, 162)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbarcode, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(49, 49, 49)
                        .addComponent(jLabel3)
                        .addGap(4, 4, 4)
                        .addComponent(jmoney, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jButton2)))
                .addGap(31, 31, 31))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jLabel7)))
                .addGap(3, 3, 3)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jLabel5)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1))
                    .addComponent(arKikloforias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel4)))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbarcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jmoney, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jButton2)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton4)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      if ( !(em.getTransaction().isActive()) ){
            em.getTransaction().begin();
        }
            em.getTransaction().commit();
      
      
            dispose();
        
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jbarcodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbarcodeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbarcodeActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        if (em.getTransaction().isActive()){
            em.getTransaction().rollback();
            em.getTransaction().begin();}
        dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       
        //Βρες το όχημα
        query1 = em.createQuery("select v from Vehicle v where v.regNumber=:regNumber").setParameter("regNumber", arKikloforias.getText());
          List <Vehicle> list1 = query1.getResultList();
        
      if (list1.size()==1) {// Υπάρχει το όχημα?
                         
            v = list1.get(0);
            int bar=0;
            try{
            bar=Integer.parseInt(jbarcode.getText()); }
             catch(NumberFormatException nfe){messageBox.YesNo("Λάθος στοιχεία. ");}
             //Συγκρινε την κάρτα του οχήματος με αυτό που πληκτρολογήθηκε
           if (v.getCardId().getBarcode()==bar){// το βρήκε
               jButton1.setVisible(true);
               jButton4.setVisible(true);
               boolean exei_lefta=true;
              
               jLabel3.setVisible(true);
               jmoney.setVisible(true);
               jButton2.setVisible(true);
               jmoney.setText(v.getCardId().getBalance().toString());
               
                if (v.getCardId().getBalance()<5){
                 int answer=  messageBox.YesNo("Το υπόλοιπο είναι κάτω από 5€. θέλετε να προχωρήσετε σε πληρώμη;");
                   if (answer==0){
                        pay(v);//μεθοδος για την πληρωμη της κάρτας
                     }
               }//Τελος Χρηματα <5€
               
               // Χρεωση
               // Ποσες διελέυσεις έχει κάνει αυτόν τον μήνα
               
               Calendar calendar = Calendar.getInstance();
               Date today=calendar.getTime();
               int running_month=today.getMonth();
               SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
               
               
                collectionquery = em.createQuery("select co from Collection co where co.cardId=:cardid").setParameter("cardid", v.getCardId());
                
               List <model.Collection> colllist = collectionquery.getResultList();
               Iterator ipr =colllist.iterator();
                
              
              int how_many_times=0;
              while(ipr.hasNext()) {// Πέρνα από όλα τα στοιχεια της λιστας με τα collections του συγκεκριμένου οχηματος
                  // βρες πόσεσ διελευσεις εκανε αυτον το μήνα
                    model.Collection co = ( model.Collection) ipr.next();
                    Date  d=co.getPassTimestamp() ;
                    if (d.getMonth()==running_month){
                        how_many_times++;
                       }
                        
                   } //Τελος while
              jLabel5.setVisible(true);
              jLabel5.setText("Διέλευσεις αυτόν τον μήνα: "+how_many_times);
              
              jLabel4.setVisible(true);
              
               // Αν εχει τον απαιτουμενο αριθμό διελέυσεων
                if (how_many_times>=v.getProgramId().getFreePassRequired()){
                     // και αν δεν εχει εξαντλησει το δωρεαν
                        if (how_many_times<v.getProgramId().getFreePass()){
                         jLabel4.setText("Δωρεαν διέλευση ");
                            JOptionPane.showMessageDialog(this, "Δωρεαν διέλευση ","Προσοχή",JOptionPane.ERROR_MESSAGE);
                              lefta= BigDecimal.valueOf(0.0);             
                           }

                        else{ //εχουν εξαντληθεί τα δωρεάν άρα χρέωση

                                   exei_lefta= xreosi(); // Η ρουτίνα ελέγχει αν υπάρχει επαρκές υπόλοιπο στην κάρτα
                                   // και κάνει την χρέωση στην κάρτα
                           }
                     }
                
                
                else{//Δεν εχει τον απαιτουμενο αριθμό διελέυσεων gia δωρεαν
                   
                    System.out.println("Δεν εχει δωρεαν");
                        exei_lefta= xreosi();// Η ρουτίνα ελέγχει αν υπάρχει επαρκές υπόλοιπο στην κάρτα
                                   // και κάνει την χρέωση στην κάρτα
                
                
                }
             if (exei_lefta){ 
              System.out.println(" εχειlefta "+lefta); 
              new_collection= new model.Collection();
              em.persist(new_collection);
              new_collection.setCardId(v.getCardId());
              new_collection.setTollCollected(lefta);
              new_collection.setPassTimestamp(today);
              new_collection.setStationId(Session.loginUser.getStationId());
             
             }
              
              
           }
               else  { //Δεν το βρηκε
                 JOptionPane.showMessageDialog(this, "Λάθος αριθμός κάρτας(Barcode) ","Προσοχή",JOptionPane.ERROR_MESSAGE);
                 jbarcode.requestFocusInWindow();  
           }
           
        }
          else{
           JOptionPane.showMessageDialog(this, "Λάθος αριθμός κυκλοφορίας","Προσοχή",JOptionPane.ERROR_MESSAGE);
           arKikloforias.requestFocusInWindow();
          }
          
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        pay(v);        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CollectTollForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CollectTollForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CollectTollForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CollectTollForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CollectTollForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField arKikloforias;
    private javax.persistence.Query collectionquery;
    private java.util.List colllist;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jbarcode;
    private javax.swing.JTextField jmoney;
    private java.util.List list1;
    private javax.persistence.Query query1;
    // End of variables declaration//GEN-END:variables


public   void pay (final Vehicle v){
     PaymentForm pf = new PaymentForm(v);
        pf.setVisible(true);
       
          em.persist(v);
          pf.setVisible(true);
          thisFrame=this;
          thisFrame.setEnabled(false);
       
        pf.addWindowListener(new WindowListener() {
            public void windowClosed(WindowEvent arg0) {
                System.out.println("Window close event occur");
                if (((MyWindowEvent)arg0).exitAndSave){
                    
                  list1.set(0,v);
                  thisFrame.setEnabled(true);  
                  jmoney.setText(v.getCardId().getBalance().toString());
                 // em.getTransaction().commit();
                }
                else{
                    thisFrame.setEnabled(true);
                    em.getTransaction().rollback();
                    em.getTransaction().begin();
                    java.util.Collection data = query1.getResultList();
                    for (Object entity : data) {
                        em.refresh(entity);
                    }
                    list1.clear();
                    list1.addAll(data);
                }
            }
        
            public void windowActivated(WindowEvent arg0) {
                System.out.println("Window Activated");
            }
            public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
            }
            public void windowDeactivated(WindowEvent arg0) {
                System.out.println("Window Deactivated");
            }
            public void windowDeiconified(WindowEvent arg0) {
                System.out.println("Window Deiconified");
            }
            public void windowIconified(WindowEvent arg0) {
                System.out.println("Window Iconified");
            }
            public void windowOpened(WindowEvent arg0) {
                System.out.println("Window Opened");
            }
        }
       );            
    
}
public  model.Collection new_collection;
 Vehicle v;
 JFrame thisFrame;
 
 public boolean xreosi(){ 
     boolean exei_lefta=true;
     lefta=null;
     
      List <ProgramRate> listp=v.getProgramId().getProgramRateList();
                    for( int i=0;i<=listp.size()-1;i++){//Ψαξε όλους τους σταθμούς και βρες
                       
                            //Ποιος σταθμος είναι αυτός που περναέι το όχημα
                        if (listp.get(i).getStationId()== Session.loginUser.getStationId());{
                                        //Αν υπάρχει υπόλοιπο στην κάρτα μεγαλύτερο από την πιθανή χρέωση τότε
                                    if( v.getCardId().getBalance().doubleValue()>=listp.get(i).getRate().doubleValue()){
                                      //Αφάιρεσε το ποσό από την κάρτα
                                        v.getCardId().setBalance(v.getCardId().getBalance()-listp.get(i).getRate().doubleValue());
                                        lefta=listp.get(i).getRate();
                                        jLabel4.setText("Διέλευση με χρέωση :" +lefta);
                                        exei_lefta=true; //Βοηθητική μεταβλητή για τον έλεγχο των χρημάτων
                                       }
                                    else{// Τα λεφτά δεν φτάνουν
                                        exei_lefta=false;
                                        JOptionPane.showMessageDialog(this, "Δεν υπάρχει επαρκές υπόλοιπο. Φορτίστε την κάρτα","Προσοχή",JOptionPane.ERROR_MESSAGE);
                                    }
                               break;
                            }

                    }//Τελός for
     
     return exei_lefta;
 }
 
 public  BigDecimal lefta = null;
}
